from django.http import HttpResponse
from django.shortcuts import render_to_response, redirect
from django.template import RequestContext
from settings import DEFAULT_FROM_EMAIL, EMAIL_CONTACT_RECEIVER
from studenthoods.apps.common.email_utils import send_email
from studenthoods.apps.feedback.forms import FeedbackForm
from studenthoods.apps.feedback.models import Feedback

def feedback(request):    
    if request.method == 'POST':    
        form = FeedbackForm(request.POST)
        
        if form.is_valid():
            feedback = Feedback()
            feedback.first_name = form.cleaned_data['first_name']
            feedback.surname = form.cleaned_data['surname']
            feedback.email = form.cleaned_data['email']
            feedback.comment = form.cleaned_data['comment']
            feedback.save()
            
            msg = feedback.email + ' has given StudentHoods feedback... '+ feedback.comment
            
            send_email('StudentHoods: Feedback', msg, DEFAULT_FROM_EMAIL, EMAIL_CONTACT_RECEIVER)

            return redirect('feedback_submitted')

    else:
        form = FeedbackForm()
    
    return render_to_response('feedback/templates/feedback.html', 
                              {'form': form}, 
                              context_instance=RequestContext(request))
    
def feedback_submitted(request):
    return render_to_response('feedback/templates/feedback_submitted.html', {},
                              context_instance=RequestContext(request))